/*-----------------------------------------------------------------------------
@author
john
@author

@desc
File: driver.cpp
This is the test suite driver for unit tests. I'm not sure this is how we want
to perform the tests, but I'm putting it in anyway.
@desc

02/21/11  jnr	Created
02/21/11  jnr	Added the framework for the first unit test (incomplete)
02/23/11  jnr	Completed Test#1
03/01/11  jnr   Added additional unit tests for the getPath() method
				began restructuring of unit test labeling to decouple
				the numbering scheme.
-----------------------------------------------------------------------------*/

#include "stdafx.h"
#include <direct.h>
#include <iostream>
#include "ProjectPath.h"
#include "Comment.h"
#include "HtmlProcess.h"
#include "ProcessInput.h"
#include "ProcessInputFiles.h"
#include "globals.h"
using namespace std;

int main()
{	
	int iTestNum = 1;
		
	//-----------------------------------------------------------------
	char cCurrentPath[MAX_PATH];
	char *buffer = NULL;
	//getPath() Test: No path supplied from Addin (FORCED FAILURE)
	bool bVal = getPath(buffer, NULL);
	printf("getPath()\t Test #%d\t %s\n", iTestNum++, !bVal ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//getPath() Test: No "\\" found in path from Addin (FORCED FAILURE)
	bVal = getPath(buffer, "c:");
	printf("getPath()\t Test #%d\t %s\n", iTestNum++, !bVal ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//getPath() Test: only "\\" found in path from Addin
	//this has same effect as allocating new buffer of size 0 (FORCED FAILURE)
	bVal = getPath(buffer, "\\");
	printf("getPath()\t Test #%d\t %s\n", iTestNum++, !bVal ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//getPath() Test: ensure the result of getPath() is not NULL
	//we need to seed the initial project path. In our actual project,
	//this would come from the Addin itself.
	if (!_getcwd (cCurrentPath, sizeof(cCurrentPath)))
	{
		printf("_getcwd() FAILED: aborting test number %d\n", iTestNum); 	
	}
	else
	{
		bVal = getPath(buffer, cCurrentPath);
		printf("getPath()\t Test #%d\t %s\n", iTestNum++, bVal ? "PASSED" : "FAILED");
	}

	//-----------------------------------------------------------------
	//ensure Comment obj is processed correctly
	//printf("Starting TEST #2...\n");
	Comment *z_com = new Comment();
	vector<string> *z_comment_list = new vector<string>();
	z_comment_list->push_back("1Dogs ");
	z_comment_list->push_back("like ");
	z_comment_list->push_back("my ");
	z_comment_list->push_back("pizza. ");
	z_com->setComments("test.cpp", z_comment_list); 	

	char *sentence = NULL;
	sentence = formatComment(z_com);
	printf("formatComment()\t Test #%d\t %s\n",iTestNum++, sentence ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//ensure null pointer to vector of Comments is processed correctly
	//printf("Starting TEST #3...\n");
	vector<Comment *> *vec_Comments = NULL;
	//std::cout << (int)vec_Comments->size();
	
	printf("createDoc()\t Test #%d\t %s\n",iTestNum++, createDoc(vec_Comments) ? "FAILED" : "PASSED");

	//-----------------------------------------------------------------
	//ensure empty vector of Comments is processed correctly
	//printf("Starting TEST #4...\n");
	vec_Comments = new vector<Comment *>();
	printf("createDoc()\t Test #%d\t %s\n",iTestNum++, createDoc(vec_Comments) ? "FAILED" : "PASSED");

	//-----------------------------------------------------------------
	//TEST 5: process a one entry Comment vector
	//printf("Starting TEST #5...\n");
	Comment *a_com = new Comment();
	vector<string> *a_comments = new vector<string>();
	a_comments->push_back("1Find ");
	a_comments->push_back("me ");
	a_comments->push_back("an ");
	a_comments->push_back("immovable ");
	a_comments->push_back("object. ");
	a_com->setComments("test.cpp", a_comments);

	Comment *b_com = new Comment();
	vector<string> *b_comments = new vector<string>();
	b_comments->push_back("1And ");
	b_comments->push_back("I ");
	b_comments->push_back("will ");
	b_comments->push_back("put ");
	b_comments->push_back("that ");
	b_comments->push_back("question ");
	b_comments->push_back("to ");
	b_comments->push_back("rest. ");
	b_com->setComments("test.cpp", b_comments);

	vec_Comments->push_back(a_com);
	vec_Comments->push_back(b_com);

	printf("createDoc()\t Test #%d\t %s\n",iTestNum++, createDoc(vec_Comments) ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//process a multiple entry vector of Comments
	//printf("Starting TEST #6...\n");
	Comment *c_com = new Comment();
	vector<string> *c_comments = new vector<string>;
	c_comments->push_back("1Can't see the forest for the trees.");
	c_com->setComments("forest.h", c_comments);

	Comment *d_com = new Comment();
	vector<string> *d_comments = new vector<string>;
	d_comments->push_back("1Glass half full.");
	d_com->setComments("glass.h", d_comments);
	
	Comment *e_com = new Comment();
	vector<string> *e_comments = new vector<string>;
	e_comments->push_back("1Rum 'n Coke.");
	e_com->setComments("drink.cpp", e_comments);

	Comment *f_com = new Comment();
	vector<string> *f_comments = new vector<string>;
	f_comments->push_back("1Fat Tire.");
	f_com->setComments("drink.cpp", f_comments);

	Comment *g_com = new Comment();
	vector<string> *g_comments = new vector<string>;
	g_comments->push_back("1Diet Coke.");
	g_com->setComments("drink.h", g_comments);

	vector<Comment *> *multi_vec = new vector<Comment *>();
	multi_vec->push_back(c_com);
	multi_vec->push_back(d_com);
	multi_vec->push_back(e_com);
	multi_vec->push_back(f_com);
	multi_vec->push_back(g_com);
	printf("createDoc()\t Test #%d\t %s\n",iTestNum++, createDoc(multi_vec) ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------
	//TEST 7: ensure Comment color processing works
	Comment *h_com = new Comment();
	vector<string> *h_comments = new vector<string>();
	h_comments->push_back("4This should be red.");
	h_com->setComments("red.cpp", h_comments);

	vector<Comment *> *red_vec = new vector<Comment *>();
	red_vec->push_back(h_com);

	printf("createDoc()\t Test #%d\t %s\n",iTestNum++, createDoc(red_vec) ? "PASSED" : "FAILED");

	//-----------------------------------------------------------------  
	//TEST 11-16: ensure processing comment tags in the file works
	//            processTag(filename, Source, SupportedTagArray[i],line);
	allComments  = new vector<Comment *>();
	if (!_getcwd (cCurrentPath, sizeof(cCurrentPath)))
	{
		printf("_getcwd() FAILED: aborting test number %d\n", iTestNum); 	
	}
	else
	{
		strcat(cCurrentPath, "\\testCom1.txt");
		std::ifstream Source;        // input files.
		
		Source.open ( cCurrentPath );
		if(!Source)
		{
			printf("Source.open() FAILED: aborting test number %d\n", iTestNum);
			
		}
		else
		{
			string line;
			bVal = false;
			for(int i=0; i<NUM_SUPPORTED_TAGS; i++)
			{
				Source.clear();
				Source.seekg(0); //reset file pointer to beginning
				while ( !Source.eof() )
				{	
					allComments->clear(); //clean out the vector before each run
					getline(Source, line);
											
					bVal = processTag(cCurrentPath,Source,SupportedTagArray[i],line);
					if(!bVal)
						continue;
					//if the vector contains something, that means we correctly caught the desired tag
					printf("ProcessTag(%s)\t Test #%d\t %s\n",SupportedTagArray[i].c_str(),iTestNum++, allComments->size() > 0 ? "PASSED" : "FAILED");
				}
				
			}
		}
		Source.close();
	    
	}
    
	//----------------------------------------------------------------- 
	//TEST 17 : ensure reading all file (.cpp/.h) works
	//          read_comments ( char *pathToProject, WIN32_FIND_DATA fd );
    if (!_getcwd (cCurrentPath, sizeof(cCurrentPath)))
	{
		printf("_getcwd() FAILED: aborting test number %d\n", iTestNum); 	
	}
	else
	{
	char cpp_File[MAX_PATH]; 
	char h_File[MAX_PATH]; 
	sprintf(cpp_File,cCurrentPath);	// set current directory for reading file .cpp/.h
	sprintf(h_File,cCurrentPath);
	strcat(cpp_File, "\\*.cpp");	// set all file .cpp
	strcat(h_File, "\\*.h");
	HANDLE hSearch = NULL;
	WIN32_FIND_DATA fd;	
	
	// checking file .cpp/.h
	hSearch = FindFirstFile (cCurrentPath,&fd);
	bVal = processFile(cCurrentPath, fd);
	
	printf("ProcessFile()ready reading file .cpp/.h\t Test #%d\t %s\n",iTestNum++, !bVal ? "PASSED" : "FAILED");
	
	// opening first .cpp file and reading the comments
	hSearch = FindFirstFile (cpp_File,&fd);
	bVal = processFile(cpp_File, fd);
	if (bVal)
		read_comments(cCurrentPath,fd);
	printf("read_comments() read comments file .cpp\t Test #%d\t %s\n",iTestNum++, bVal ? "PASSED" : "FAILED");


	// opening first .h file and reading the comments
	hSearch = FindFirstFile (h_File,&fd);
	bVal = processFile(h_File, fd);
	if (bVal)
		read_comments(cCurrentPath,fd);
	printf("read_comments() read comments file .h\t Test #%d\t %s\n",iTestNum++, bVal ? "PASSED" : "FAILED");
	
	}
	//----------------------------------------------------------------- 
	if (!_getcwd (cCurrentPath, sizeof(cCurrentPath)))
	{
		printf("_getcwd() FAILED: aborting test number %d\n", iTestNum); 	
	}
	else
	{
		HANDLE hSearch = NULL;
	    WIN32_FIND_DATA fd;	
		char CppFile[MAX_PATH]; 
		char HFile[MAX_PATH]; 
		sprintf(CppFile,cCurrentPath);
		sprintf(HFile,cCurrentPath);
		strcat(CppFile, "\\*.cpp");
		strcat(HFile, "\\*.h");

		//find no file extension (FORCED FAILURE)
		hSearch = FindFirstFile (cCurrentPath,&fd);
		bVal = processFile(cCurrentPath, fd);
		printf("ProcessFile()\t Test #%d\t %s\n",iTestNum++, !bVal ? "PASSED" : "FAILED");

		//find first .cpp file
		hSearch = FindFirstFile (CppFile,&fd);
		bVal = processFile(CppFile, fd);
		printf("ProcessFile()\t Test #%d\t %s\n",iTestNum++, bVal ? "PASSED" : "FAILED");
		
		//find first .h file
		hSearch = FindFirstFile (HFile,&fd);
		bVal = processFile(HFile, fd);
		printf("ProcessFile()\t Test #%d\t %s\n",iTestNum++, bVal ? "PASSED" : "FAILED");
	}
	//Add new Tests here...


	system("pause");
}